package com.mr.cai.main.bean;

import android.content.ContentValues;

import com.mr.cai.main.bean.base.MrCaiSubBaseBean;

import org.litepal.crud.DataSupport;

import java.util.List;

/**
 * Created by hai8108 on 16/2/11.
 */
public class SearchHistoryBean extends DataSupport implements MrCaiSubBaseBean {
    private String keyWord;
    private String keyId;
    private long addTime;

    public String getKeyWord () {
        return keyWord;
    }

    public void setKeyWord (String keyWord) {
        this.keyWord = keyWord;
    }

    public long getAddTime () {
        return addTime;
    }

    public void setAddTime (long addTime) {
        this.addTime = addTime;
    }

    public String getKeyId () {
        return keyId;
    }

    public void setKeyId (String keyId) {
        this.keyId = keyId;
    }

    public static boolean saveData(SearchHistoryBean mBean){
        if(has(mBean)){
            ContentValues values = new ContentValues();
            values.put("keyWord", mBean.getKeyWord());
            values.put("addTime", mBean.getAddTime());
            DataSupport.updateAll(SearchHistoryBean.class, values, "keyWord = ?", mBean.getKeyWord());
        } else {
           mBean.save();
        }
        return true;
    }
    public static void deleteData(SearchHistoryBean mBean){
        DataSupport.deleteAll(SearchHistoryBean.class, "keyWord = ?", mBean.getKeyWord());
    }
    public static int deleteAllData(){
        return DataSupport.deleteAll(SearchHistoryBean.class);
    }

    public static List<SearchHistoryBean> findDataAll(){
        List<SearchHistoryBean> newsList = DataSupport.where("keyWord <> ?", System
                .currentTimeMillis()+"").find
                (SearchHistoryBean.class);
        if(newsList != null && newsList.size()> 0){
            if(newsList.size() >=10) {
                return newsList.subList(0, 10);
            }else{
                return newsList;
            }
        } else{
            return null;
        }
    }
    public static boolean has(SearchHistoryBean mBean){
        return DataSupport.where("keyWord = ?", mBean.getKeyWord()).count(SearchHistoryBean.class) > 0;
    }
}
